<?php

namespace teach\service;

use Yii;

class Pay
{
    /**
     * 查找一条产品的详细信息
     * @param $parameter
     * @param $param
     * @return array|false
     */
    public function productDetail($parameter, $param)
    {
        $sql = "SELECT  s.status as s_status,s.product_no, s.level_one, s.level_two,s.stock,s.value,s.price, p.id, p.is_up , p.freight, p.mid, p.label FROM mall_product_specs as s LEFT JOIN mall_product as p ON p.id = s.pid WHERE  s.id = {$parameter} AND p.id= {$param} AND p.status = 1";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 根据pid查询产品
     * @param $param
     * @return array|false
     */
    public function productDetails($param)
    {
        $sql = "SELECT  s.product_no, s.level_one, s.level_two,s.stock,s.value,s.price, p.id, p.is_up , p.freight, p.mid, p.label FROM mall_product_specs as s LEFT JOIN mall_product as p ON p.id = s.pid WHERE p.id= {$param} AND p.status = 1";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 用户购买插入订单事务
     * @param $order
     * @param $orderList
     * @return bool
     */
    public function orderInsert($order, $orderList)
    {

//        Yii::$app->db->createCommand()->insert('mall_order', $order)->execute();
//        Yii::$app->db->createCommand()->insert('mall_order_list', $orderList)->execute();

        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            //插入订单总表
            $db->createCommand()->insert('mall_order', $order)->execute();
            //插入订单分表
            $db->createCommand()->insert('mall_order_list', $orderList)->execute();
            $transaction->commit();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }

    /**
     * 购物车发起购买
     * @param $order
     * @param $orderList
     * @return bool
     */
    public function orderCartInsert($order, $orderList)
    {
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            //插入订单总表
            $db->createCommand()->insert('mall_order', $order)->execute();
            //插入订单分表
            $db->createCommand()->batchInsert('mall_order_list', [
                'mid',
                'pid',
                'out_trade_no',
                'logistic',
                'price',
                'count',
                'create_time',
                'total_fee',
                'uid',
                'value',
                'sku_id',
                'amount',
                'level_one',
                'level_two',
                'product_no'

            ], $orderList)->execute();
            $transaction->commit();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
            return false;
        }
    }


    /**
     * 查找出商品支付折扣率
     * @param $condition
     * @return array|false
     */
    public function productDiscount($condition)
    {
        $sql = "SELECT * FROM mall_discount WHERE {$condition}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 查找汇总订单
     * @param $map
     * @return array|false
     */
    public function orderOne($map)
    {
        $sql= "SELECT * FROM mall_order WHERE {$map} ";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 查询分批订单号
     * @param $mapper
     * @return array
     */
    public function orderList($mapper)
    {
        $sql = "SELECT * FROM mall_order_list WHERE {$mapper}";
        return Yii::$app->db->createCommand($sql)->queryAll();
    }

    /**
     * 将订单修改为 -2 订单异常状态
     * @param $map
     * @return int
     */
    public function orderUnusual($map)
    {
        $sql = "UPDATE mall_order SET status = -4 WHERE {$map}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 重新发起支付, 更新下订单表
     * @param $param
     * @param $map
     * @param $whereList
     * @param $parameter
     * @return int
     */
    public function orderUpdate($param, $parameter, $map, $whereList)
    {
//        $sql = "UPDATE mall_order SET {$param} WHERE {$map}";
//        return Yii::$app->db->createCommand($sql)->execute();

        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            //插入订单总表
            $sqlTotal = "UPDATE mall_order SET {$param} WHERE {$map}";
            $db->createCommand($sqlTotal)->execute();
            //插入订单分表
            $sqlList = "UPDATE mall_order_list SET {$parameter} WHERE id IN ({$whereList})";
            $db->createCommand($sqlList)->execute();
            $transaction->commit();
            return true;
        } catch (\Exception $e) {
            $transaction->rollBack();
//            var_dump($e->getMessage());
            return false;
        }
    }

    /**
     * 获取订单
     * @param $out_trade_no
     * @return array|false
     */
    public function orderGetId($out_trade_no)
    {
        $sql = "SELECT id, out_trade_no FROM mall_order WHERE out_trade_no  = '{$out_trade_no}'";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 库存减少
     * @param $sku_id
     * @param $count
     * @return int
     */
    public function productReduce($sku_id, $count)
    {
        $sql = "UPDATE mall_product_specs SET stock = stock - {$count} WHERE id = {$sku_id}";
        return Yii::$app->db->createCommand($sql)->execute();

    }

    /**
     * 将库存加回去
     * @param $sku_id
     * @param $count
     * @return int
     */
    public function productIncrease($sku_id, $count)
    {
        $sql = "UPDATE mall_product_specs SET stock = stock + {$count} WHERE id = {$sku_id}";
        return Yii::$app->db->createCommand($sql)->execute();
    }

    /**
     * 获取折扣跟规则
     * @param $mid
     * @return array|false
     */
    public function distribution($mid)
    {
        $sql = "SELECT * FROM mall_distribution WHERE mid = {$mid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 根据id查找具体的用户
     * @param $uid
     * @return array|false
     */
    public function userOne($uid)
    {
        $sql = "SELECT id, openid, unionid, recharge FROM edu_user WHERE id = {$uid}";
        return Yii::$app->db->createCommand($sql)->queryOne();
    }

    /**
     * 减去用户的支票
     * @param $uid
     * @param $recharge
     * @return int
     */
    public function userSubRecharge($uid, $recharge)
    {
        $sql = "UPDATE edu_user SET recharge = recharge - {$recharge} WHERE id = {$uid}";
        return Yii::$app->db->createCommand($sql)->execute();
    }
}